package cn.tedu.lucky.draw.server.job.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {

    /**
     * JOB构建工厂
     * 构建JOB对象
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;


    /**
     * STEP构建工厂
     * 构建Step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    //构建一个步骤对象
    @Bean
    public Step step1(){
        return stepBuilderFactory.get("step1") //Step名称
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("Hellp.World！");
                    return RepeatStatus.FINISHED; //表示步骤执行完成
                })
                .build();
    }

    //构建一个作业对象
    @Bean
    public Job job(){
        return jobBuilderFactory.get("job1") //Job名称
                .start(step1()) //添加步骤
                .build();
    }

    //通过Jobluancher启动JOB
    @Autowired
    private JobLauncher jobLauncher;

}
